<%@ include file="/html/portlet/init.jsp" %>
<%@ include file="/html/portlet/upe/estagio/css/jgrowl.jsp" %>
<%@ include file="/html/js/ui.datepicker-pt-BR.js" %>

<script language="javascript">

if(!this.Estagio){
	Estagio = {};
}

jQuery.extend(Estagio, {

	Messages : {
	
		addMessage : function(message, time){
				
			if(time == undefined)
				time = 5000;
			
			jQuery.jGrowl(message, { 
				speed:  'slow',
				life: time,
				closerTemplate: '<div style="height:26px;">[ Fechar mensagens ]</div>'
			});
		},
		
		addErrorMessage : function(message, time){
		
			if(time == undefined)
				time = 5000;
			
			jQuery.jGrowl(message, { 
				speed:  'slow',
				theme:	'jGrowl-error',
				life: 	time,
				closerTemplate: '<div style="height:26px;">[ Fechar mensagens ]</div>'
			});
		}
	},
	
	Validation : {

		validate : function(div, validationMap){
		
			var error = false;
			var errorMap = {
					counter : 0,
					messages : {}
					};
			
			jQuery.each(
				validationMap, 
				function(key, value){
		
					var inputTemp = div.find('input[name="'+key+'"],select[name="'+key+'"],textarea[name="'+key+'"]:first');

					if(value.regExp != undefined){
						
					}else{

						if(inputTemp.val() == ''){
		
							error = true;
							errorMap.counter++;
							errorMap.messages[key] = value.emptyMessage;
						}
					}
				});
		
			return errorMap;
		},

		show : function(errorMap){
			console.log(errorMap);
			jQuery.each(errorMap.messages, 
				function(key, value){
					Estagio.Messages.addErrorMessage(value);
				});
		}
	},

	Locker : function(_phase){

		this.phase = _phase;
		this.keys = {};
		var instance = this;		
		
		this.isLocked = function(){
			if(instance.keys[instance.phase] == undefined || instance.keys[instance.phase].lock == undefined){
				instance.lock();
				return false;
			}else{
				return instance.keys[instance.phase].lock;
			}
		};
		
		this.lock = function(){
			instance.keys[instance.phase] = {lock : true};
		};

		this.unlock = function(phase){
			instance.keys[instance.phase] = {lock : false};
		};
	},		
	
	Util : {

		setDatepicker : function(input){

			if(input.length){

				var a = [];
				
				jQuery.each(input, 
					function(i, item){
						a[a.length] = Estagio.Util.setDatepicker(item);
					});
				
				return a;
			}else
				/*jQuery.datepicker.regional['pt-BR']*/
				return jQuery(input).datepicker({yearRange: '-100:-5'});
		}
	},
	
	getProfessor : function(matricula){
	
		jQuery.showLoading();
		
		var jsonRequest = jQuery.ajax({
			type : "POST",
			dataType : "json",
			async : false,
			url : "<%=themeDisplay.getPathMain() %>/upe/estagio/json_entidades",
			data : "cmd=getProfessor&matricula=" + matricula
		});

		jQuery.hideLoading();
		
		if(jsonRequest.status == 200)
			return JSON.parse(jsonRequest.responseText);
		
		return null;
	},
	
	getSolicitacaoEstagio : function(codigoSolicitacao){
	
		jQuery.showLoading();
		
		var jsonRequest = jQuery.ajax({
			type : "POST",
			dataType : "json",
			async : false,
			url : "<%=themeDisplay.getPathMain() %>/upe/estagio/json_entidades",
			data : "cmd=getSolicitacaoEstagio&codigoSolicitacao=" + codigoSolicitacao
		});

		jQuery.hideLoading();
		
		if(jsonRequest.status == 200)
			return JSON.parse(jsonRequest.responseText);
		
		return null;
	}

});

</script>